//
//  WGDay.m
//  WorkGo
//
//  Created by 李树志 on 2017/9/30.
//  Copyright © 2017年 李树志. All rights reserved.
//

#import "WGDay.h"
#import "WGCoreDataManager.h"

@interface WGDay()

@end

@implementation WGDay

@dynamic number;
@dynamic date;
@dynamic lunarYear;
@dynamic lunar;
@dynamic weekday;
@dynamic zodiac;
@dynamic holiday;
@dynamic suit;
@dynamic avoid;
@dynamic weekNum;
@dynamic isWork;

@synthesize publicHolidays = _publicHolidays;
@synthesize publicWorkdays = _publicWorkdays;

- (instancetype)initWithDict:(NSDictionary *)dict
{
    NSManagedObjectContext *context = [WGCoreDataManager sharedManager].managedObjectContext;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"WGDay" inManagedObjectContext:context];
    fetchRequest.entity = entity;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date == %@", dict[@"date"]];
    fetchRequest.predicate = predicate;
    NSError *error = nil;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    if (error) {
        NSLog(@"Core Data Fetch Error: %@", error);
    }
    if (fetchedObjects.count > 0) {
        self = fetchedObjects.firstObject;
    } else {
        self = [super initWithEntity:entity insertIntoManagedObjectContext:context];
    }
    if (self) {
        self.date = dict[@"date"];
        self.number = [self _getNumberWith:self.date];
        self.lunarYear = dict[@"lunarYear"];
        self.lunar = dict[@"lunar"];
        self.weekday = dict[@"weekday"];
        self.weekNum = [self _convertWeekdayToNum:self.weekday];
        self.zodiac = dict[@"zodiac"];
        self.suit = dict[@"suit"];
        self.avoid = dict[@"avoid"];
        self.holiday = dict[@"holiday"];
        self.isWork = [self _checkTodayIsWorkWith:self.date weekNum:self.weekNum];
    }
    [[WGCoreDataManager sharedManager] saveContext];
    return self;
}

+ (instancetype)dayWithDict:(NSDictionary *)dict
{
    return [[WGDay alloc] initWithDict:dict];
}

- (BOOL)_checkTodayIsWorkWith:(NSString *)dateString weekNum:(int16_t)weekNum
{
    if ([self.publicWorkdays containsObject:dateString]) {
        return YES;
    }
    if ([self.publicHolidays containsObject:dateString]) {
        return NO;
    }
    if (weekNum == 6 || weekNum == 7) {
        return NO;
    }
    return YES;
}

- (NSArray<NSString *> *)publicHolidays {
    return @[
             // 元旦
             @"2017-01-01",
             @"2017-01-02",
             // 春节
             @"2017-01-27",
             @"2017-01-28",
             @"2017-01-29",
             @"2017-01-30",
             @"2017-01-31",
             @"2017-02-01",
             @"2017-02-02",
             // 清明
             @"2017-04-02",
             @"2017-04-03",
             @"2017-04-04",
             // 劳动节
             @"2017-05-01",
             // 端午节
             @"2017-05-28",
             @"2017-05-29",
             @"2017-05-30",
             // 国庆节
             @"2017-10-01",
             @"2017-10-02",
             @"2017-10-03",
             // 中秋节
             @"2017-10-04",
             @"2017-10-05",
             @"2017-10-06",
             @"2017-10-07",
             @"2017-10-08",
             ];
}

- (NSArray<NSString *> *)publicWorkdays {
    return @[
             // 春节补班
             @"2017-01-22",
             @"2017-02-04",
             // 清明补班
             @"2017-04-01",
             // 端午补班
             @"2017-05-27",
             // 国庆补班
             @"2017-09-30",
             ];
}

- (int16_t)_getNumberWith:(NSString *)dateString {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSDate *date = [formatter dateFromString:dateString];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierRepublicOfChina];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
    NSInteger year = components.year;
    NSInteger month = components.month;
    NSInteger day = components.day;
    NSArray *leapYear = @[@31,@29,@31,@30,@31,@30,@31,@31,@30,@31,@30,@31];
    NSArray *commonYear = @[@31,@28,@31,@30,@31,@30,@31,@31,@30,@31,@30,@31];
    
    BOOL isLeapYear = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    int16_t result = 0;
    if (isLeapYear) {
        for (int i = 0; i < month - 1; i++) {
            result += [leapYear[i] integerValue];
        }
    } else {
        for (int i = 0; i < month - 1; i++) {
            result += [commonYear[i] integerValue];
        }
    }
    result += day;
    return result;
}


- (int16_t)_convertWeekdayToNum:(NSString *)weekday {
    if ([weekday isEqualToString:@"星期一"]) {
        return 1;
    }
    if ([weekday isEqualToString:@"星期二"]) {
        return 2;
    }
    if ([weekday isEqualToString:@"星期三"]) {
        return 3;
    }
    if ([weekday isEqualToString:@"星期四"]) {
        return 4;
    }
    if ([weekday isEqualToString:@"星期五"]) {
        return 5;
    }
    if ([weekday isEqualToString:@"星期六"]) {
        return 6;
    }
    if ([weekday isEqualToString:@"星期日"]) {
        return 7;
    }
    return 0;
}


@end
